import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import Unocss from "@unocss/vite";

// https://vite.dev/config/
export default defineConfig(({mode}) =>{
  const envPrefix = 'VITE_'
  const env = loadEnv(mode, process.cwd(), envPrefix)
  return {
    plugins: [
      vue(),
      vueDevTools(),
      Unocss({}),
    ],
    base: env.VITE_HOST,
    build: {
      minify: 'esbuild', // 混淆器，terser构建后文件体积更小
      rollupOptions: {
        output: {
          /* 静态资源的打包 */
          chunkFileNames: 'js/[name]-[hash].js',  // 引入文件名的名称
          entryFileNames: 'js/[name]-[hash].js',  // 包的入口文件名称
          assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体，图片等
          manualChunks(id) {
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          }
        },
      },
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
              @use "@/assets/style/variable.scss" as *;
              @use "@/assets/style/mixin.scss" as *;
            `,
        }
      }
    },
    server: {
      port: 5173,
      host: "0.0.0.0",
    },
  }
})
